package edu.hawaii.its381b.client;

import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author David Nickles
 * @date   May 16, 2012
 */
public class SplashScreen extends Composite {

	private static final int INSET = 50;
	
	private VerticalPanel panel;
	private Image splashLogoImage;
	private Button okButton;
	
	/**
	 * 
	 */
	SplashScreen() {
		super();
		
		addJSNIFullscreenEvent();
		
		int width = (Window.getClientWidth()-INSET);
		int height = (Window.getClientHeight()-INSET);
		panel = new VerticalPanel();
		panel.setWidth(width+"px");
		panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		splashLogoImage = new Image(HermitCrabResources.INSTANCE.splashLogo().getSafeUri());
		splashLogoImage.setPixelSize(width, height/2);
		
		Window.addResizeHandler(new ResizeHandler() {
			  public void onResize(final ResizeEvent event) {
				  int width = event.getWidth()-INSET;
				  int height = event.getHeight()-INSET;
				  panel.setWidth(width+"px");
				  splashLogoImage.setPixelSize(width, height/2);
			  }
		}); 
		panel.add(splashLogoImage);
		
		okButton = new Button("Start Game");
		okButton.setStyleName("gwt-Button-Actions");
		okButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				RootPanel.get().clear();
				GameEngine game = new GameEngine();
				game.getElement().setId("myGame");
				RootPanel.get().add(game);
				//goFullscreen(RootPanel.get().getElement());
			}
		});
		panel.add(okButton);
		initWidget(panel);
	}

	/**
	 * This is a hand coded native Javascript API addition for putting the browser in full screen or kiosk mode, if it is supported.
	 */
	public native void addJSNIFullscreenEvent() /*-{

	(function() {
		var 
			fullScreenApi = { 
				supportsFullScreen: false,
				isFullScreen: function() { return false; }, 
				requestFullScreen: function() {}, 
				cancelFullScreen: function() {},
				fullScreenEventName: '',
				prefix: ''
			},
			browserPrefixes = 'webkit moz o ms khtml'.split(' ');
		
		// check for native support
		if (typeof document.cancelFullScreen != 'undefined') {
			fullScreenApi.supportsFullScreen = true;
		} else {	 
			// check for fullscreen support by vendor prefix
			for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
				fullScreenApi.prefix = browserPrefixes[i];
		
				if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
					fullScreenApi.supportsFullScreen = true;
		
					break;
				}
			}
		}
		
		// update methods to do something useful
		if (fullScreenApi.supportsFullScreen) {
			fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
		
			fullScreenApi.isFullScreen = function() {
				switch (this.prefix) {	
					case '':
						return document.fullScreen;
					case 'webkit':
						return document.webkitIsFullScreen;
					default:
						return document[this.prefix + 'FullScreen'];
				}
			}
			fullScreenApi.requestFullScreen = function(el) {
				return (this.prefix === '') ? el.requestFullScreen() : el[this.prefix + 'RequestFullScreen']();
			}
			fullScreenApi.cancelFullScreen = function(el) {
				return (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
			}	
		}
		
		// jQuery plugin
		if (typeof jQuery != 'undefined') {
			jQuery.fn.requestFullScreen = function() {
		
				return this.each(function() {
					var el = jQuery(this);
					if (fullScreenApi.supportsFullScreen) {
						fullScreenApi.requestFullScreen(el);
					}
				});
			};
		}
		
		// export api
		window.fullScreenApi = fullScreenApi;	
		})();
		
			if (fullScreenApi.supportsFullScreen) {
				//$doc.getElementById("mybutton").addEventListener("click", function() {
				//	fullScreenApi.requestFullScreen($doc.getElementById("myGame"));
				//}, true);
			}
	}-*/;
	
	/**
	 * A GWT hook to call the JNSI full screen method. 
	 */
	public native void goFullscreen(Element elt) /*-{
			//fullScreenApi.requestFullScreen($doc.getElementById("myGame"));
			fullScreenApi.requestFullScreen(elt);
    }-*/;
}
